<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">

  <script src="https://cdn.bootcss.com/vue/2.6.10/vue.js"></script>
  <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
  <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
  <!-- <script src="https://cdn.bootcss.com/html2canvas/0.5.0-beta4/html2canvas.min.js"></script> -->
  <script src="../js/html2canvas.js"></script>

  <title>vhtml的方式进行截图</title>
  <style>
    .page {
      display: inline-block;
      width: auto;
      height: 400px;
      border: 1px solid #ccc;
    }

    button {
      margin: 10px 0;
    }

    .screen-shot {
      width: 500px;
      height: 300px;
      border: 1px solid #e69090;
      background-size: cover;
    }

    .screen-shot img {
      width: 100%;
      height: 100%;
    }
  </style>
</head>

<body>
  <div id="app">
    <div class="page" id="page">
    </div>

    <p>
      <button @click="screenShot">截图</button>
    </p>

    <div class="screen-shot" :style="{backgroundImg: `url(${cropImg})`}">
      <img :src="cropImg" alt="">
    </div>
  </div>
  <script>
    function loadXMLString(txt) {
      try //Internet Explorer
      {
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = "false";
        xmlDoc.loadXML(txt);
        return (xmlDoc);
      } catch (e) {
        try //Firefox, Mozilla, Opera, etc.
        {
          parser = new DOMParser();
          xmlDoc = parser.parseFromString(txt, "text/xml");
          return (xmlDoc);
        } catch (e) {
          alert(e.message)
        }
      }
      return (null);
    }

    var vm = new Vue({
      el: '#app',
      data() {
        return {
          html: '',
          cropImg: null
        }
      },
      methods: {
        screenShot: function () {
          html2canvas(document.getElementById('page'), {
            backgroundColor: '#fff'
          }).then(canvas => {
            var imgData = canvas.toDataURL("image/jpeg");
            this.cropImg = imgData;
          });
        }
      },
      mounted() {
        axios.get('http://172.18.188.58:84//1156841461913751552.html?n=1569394256140')
          .then(res => {
            let $page = $(res.data);
            $('#page').empty().append($page);
          })
          .catch(err => {
            this.html = `<p>载入失败</p>${err}`;
          });
      }
    })
  </script>
</body>

</html>